package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.enums.BusType;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.TicketCommonService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TicketBusinessMapper;
import com.ruoyi.system.domain.TicketBusiness;
import com.ruoyi.system.service.ITicketBusinessService;

/**
 * 商户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-15
 */
@Service
public class TicketBusinessServiceImpl implements ITicketBusinessService
{
    @Autowired
    private TicketBusinessMapper ticketBusinessMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private TicketCommonService commonService;

    /**
     * 查询商户信息
     *
     * @param businessNo 商户信息主键
     * @return 商户信息
     */
    @Override
    public TicketBusiness selectTicketBusinessByBusinessNo(String businessNo)
    {
        return ticketBusinessMapper.selectTicketBusinessByBusinessNo(businessNo);
    }

    @Override
    public TicketBusiness selectTicketBusinessByBusinessName(String businessName) {
        return ticketBusinessMapper.selectTicketBusinessByBusinessName(businessName);
    }

    /**
     * 查询商户信息列表
     *
     * @param ticketBusiness 商户信息
     * @return 商户信息
     */
    @Override
    public List<TicketBusiness> selectTicketBusinessList(TicketBusiness ticketBusiness)
    {
//        return ticketBusinessMapper.selectTicketBusinessList(ticketBusiness);
        List<TicketBusiness> allBusinessByCurrentUser = commonService.getAllBusinessByCurrentUser();
        List<String> allBusinessByCurrentUserNos = allBusinessByCurrentUser.stream().map(TicketBusiness::getBusinessNo).collect(Collectors.toList());
        List<TicketBusiness> ticketBusinesses = ticketBusinessMapper.selectTicketBusinessList(ticketBusiness);
        List<String> ticketBusinessesNos = ticketBusinesses.stream().map(TicketBusiness::getBusinessNo).collect(Collectors.toList());
        ticketBusinessesNos.retainAll(allBusinessByCurrentUserNos);
        return allBusinessByCurrentUser.stream().filter(p->ticketBusinessesNos.contains(p.getBusinessNo())).collect(Collectors.toList());
    }

    /**
     * 新增商户信息
     *
     * @param ticketBusiness 商户信息
     * @return 结果
     */
    @Override
    public int insertTicketBusiness(TicketBusiness ticketBusiness)
    {
        if(StringUtils.isEmpty(ticketBusiness.getBusinessNo())){
            ticketBusiness.setBusinessNo(UUID.randomUUID().toString());
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ticketBusiness.setCreateBy(loginUser.getUsername());
        ticketBusiness.setCreateTime(DateUtils.getNowDate());
        int business = ticketBusinessMapper.insertTicketBusiness(ticketBusiness);
        //同步新增商户用户
        insertSysUser(ticketBusiness);
        return business;
    }

    /**
     * 同步新增商户对应的用户
     * @param ticketBusiness
     * @return
     */
    public int insertSysUser(TicketBusiness ticketBusiness){
        SysUser businessUser = new SysUser();
        String identityNo = ticketBusiness.getIdentityNo();
        String businessPhone = ticketBusiness.getBusinessPhone();
        //商户登录账户默认为手机号码
        businessUser.setUserName(businessPhone);
        businessUser.setNickName(ticketBusiness.getBusinessName());
        businessUser.setPhonenumber(businessPhone);
        businessUser.setSex("0");
        businessUser.setRoleId(100L);
        Long[] roleIds = new Long[1];
        roleIds[0] = 100L;
        businessUser.setRoleIds(roleIds);
        //商户自动设置为当前登录人(默认客户经理)下面的部门
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //创建对应商户名称的部门名称并将商户自动归属为本部门
        SysDept sysDept = new SysDept();
        long countDept = deptService.countDept();
        long currentDeptId = countDept + 1L;
        sysDept.setDeptId(currentDeptId);
        sysDept.setDeptName(ticketBusiness.getBusinessName());
        sysDept.setParentId(loginUser.getDeptId());
        deptService.insertDept(sysDept);
        businessUser.setDeptId(currentDeptId);
        //商户用户备注为唯一身份号，便于联动删除与唯一标识
        businessUser.setRemark(identityNo);
        String substring = identityNo.substring(identityNo.length() - 8);
        System.out.println(substring);
        //商户类型为商户登录密码默认为营业执照号后8位,个人类型为身份证号码后6位
        if(ticketBusiness.getBusinessType().equalsIgnoreCase(BusType.merchant.name())){
            String encryptPassword = SecurityUtils.encryptPassword(substring);
            businessUser.setPassword(encryptPassword);
            System.out.println(encryptPassword);
        }else {
            String substringPwd = identityNo.substring(identityNo.length() - 6);
            businessUser.setPassword(SecurityUtils.encryptPassword(substringPwd));
            System.out.println(substringPwd);
        }
        return userService.insertUser(businessUser);
    }

    /**
     * 修改商户信息
     *
     * @param ticketBusiness 商户信息
     * @return 结果
     */
    @Override
    public int updateTicketBusiness(TicketBusiness ticketBusiness)
    {
        ticketBusiness.setUpdateTime(DateUtils.getNowDate());
        return ticketBusinessMapper.updateTicketBusiness(ticketBusiness);
    }

    /**
     * 批量删除商户信息
     *
     * @param businessNos 需要删除的商户信息主键
     * @return 结果
     */
    @Override
    public int deleteTicketBusinessByBusinessNos(String[] businessNos)
    {
        return ticketBusinessMapper.deleteTicketBusinessByBusinessNos(businessNos);
    }

    /**
     * 删除商户信息信息
     *
     * @param businessNo 商户信息主键
     * @return 结果
     */
    @Override
    public int deleteTicketBusinessByBusinessNo(String businessNo)
    {
        TicketBusiness ticketBusiness = ticketBusinessMapper.selectTicketBusinessByBusinessNo(businessNo);
        String identityNo = ticketBusiness.getIdentityNo();
        //删除商户,商户用户状态禁用
        SysUser sysUser = userService.selectByRemark(identityNo);
        userService.deleteUserById(sysUser.getUserId());
        return ticketBusinessMapper.deleteTicketBusinessByBusinessNo(businessNo);
    }
}
